<%@page import="com.ddd.entity.Negoitem"%>
<%@page import="com.ddd.entity.Contract"%>
<%@page import="com.ddd.entity.Publisher"%>
<%@page import="com.ddd.entity.Negotiation"%>
<%@page import="com.ddd.service.NegotiationService"%>
<%@page import="com.ddd.context.Context"%>
<%@page import="com.ddd.service.NegoitemService"%>
<%@page import="com.ddd.service.ContractService"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ddd.service.PublisherService"%>
<%@page import="com.ddd.util.Util"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Util.checkBEAuthorized(request, response);

    PublisherService publisherService = new PublisherService(request);
    ArrayList publisherList = publisherService.loadAll();

    ContractService contractService = new ContractService(request);
    ArrayList contractList = contractService.loadAll();

    NegoitemService negoitemService = new NegoitemService(request);
    NegotiationService negotiationService = new NegotiationService(request);

    String mode = request.getParameter("mode");
    String id = request.getParameter("id");

    Negotiation negotiation = new Negotiation();
    ArrayList negoitemList = null;

    if (Util.isBlank(mode)) {
        mode = "add";
    } else if (!mode.equals("add")) {
        negotiation = negotiationService.load(id);
        negoitemList = negoitemService.loadByNegotiationID(id);
    }

    String message = request.getParameter("message");
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=Context.SITE_NAME%></title>
        <link type="text/css" href="css/style.css" rel="stylesheet" />
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>

        <script type="text/javascript">
            $(function() {
                $( "#accordion" ).accordion({collapsible: true});
            });
            
            function doUpdate (){
                $( "#dialog_update" ).dialog({
                    title: "更新版權徵集",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "更新": function() {
                            $( this ).dialog( "close" );
                            $('#form1').submit();
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
            
            function addNegoItem(){
                $( "#dialog_add_negoitem" ).dialog({
                    title: "新增合作紀錄",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "新增": function() {
                            $( this ).dialog( "close" );
                            doAddNegoItem();
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
         
            function doAddNegoItem(){
                var negoDate = $("#negoDate_new").val();
                var memo = $("#memo_new").val();
                
                $('#negoDate').val(negoDate);
                $('#memo').val(memo);
                $('#form2').submit();
            }
            
            function deleteNegoItem(negoItemID){
                var id = '<%=id%>';
                $( "#dialog_delete_negoitem" ).dialog({
                    title: "刪除合作紀錄",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "刪除": function() {
                            $( this ).dialog( "close" );
                            window.location = "../doNegoitem?mode=delete&id=" + negoItemID + "&fkNegotiationID=" + id;
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
         
            function showMessage(){
            <%if (Util.isNotBlank(message)) {%>
                    $( "#dialog_message" ).dialog({
                        modal: true,
                        title:"訊息",
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
            <%}%>
                }
        </script>
    </head>
    <body onload="showMessage();">
        <div id="wrapper">
            <div id="header">
                <div id="logo">
                    <h1><a href="#"><%=Context.SITE_NAME%></a></h1>
                </div>
            </div>
            <!-- end #header -->
            <div id="menu">
                <%@ include file="menu.jsp" %>
            </div>
            <!-- end #menu -->
            <div id="page">
                <div id="page-bgtop">
                    <div id="page-bgbtm">
                        <!-- starrt #content -->
                        <div id="content">
                            <form id="form1" name="form1" action="../doNegotiation" method="post">
                                <input type="hidden" id="mode" name="mode" value="<%=mode%>"></input>
                                <input type="hidden" id="cid" name="cid" value="<%=negotiation.getCid()%>"></input>
                                <input type="hidden" id="ctime" name="ctime" value="<%=negotiation.getCtime()%>"></input>
                                <input type="hidden" id="uid" name="uid" value="<%=negotiation.getUid()%>"></input>
                                <input type="hidden" id="utime" name="utime" value="<%=negotiation.getUtime()%>"></input>
                                <table>
                                    <%if (!mode.equals("add")) {%>
                                    <tr>
                                        <td align="left"><a href="#records">合作修改紀錄</a></td>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <%}%>
                                    <tr>
                                        <td align="left">序號</td>
                                        <%if (mode.equals("update")) {%>
                                        <td><input id="id" name="id" value="<%=negotiation.getId()%>" readonly></td>
                                            <%} else {%>
                                        <td><input id="id" name="id" value="<%=negotiation.getId()%>"></td>
                                            <%}%>
                                    </tr>
                                    <tr>
                                        <td>人員姓名</td>
                                        <td><input type="text" id="staff" name="staff" value="<%=negotiation.getStaff()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>簽約單位</td>
                                        <td><input type="text" id="authority" name="authority" value="<%=negotiation.getAuthority()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>期刊名稱</td>
                                        <td><input type="text" id="journalName" name="journalName" value="<%=negotiation.getJournalName()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>拜訪日期</td>
                                        <td><input type="text" id="visitDate" name="visitDate" value="<%=negotiation.getVisitDate()%>"></td>
                                    </tr>

                                    <tr>
                                        <td>簽約成功率</td>
                                        <td>
                                            <select id="successRate" name="successRate"> 
                                                <option value=""></option>
                                                <% for (int i = 0; i <= 100; i = i + 10) {
                                                        String rate = String.valueOf(i);
                                                        if (negotiation.getSuccessRate().equals(rate)) {
                                                %>
                                                <option value="<%=rate%>" selected="<%=rate%>"><%=rate%>%</option>
                                                <%} else {%>
                                                <option value="<%=rate%>"><%=rate%>%</option>
                                                <%}
                                                    }%>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>簽約日期</td>
                                        <td><input type="text" id="contractDate" name="contractDate" value="<%=negotiation.getContractDate()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>簽約年限</td>
                                        <td><input type="text" id="contractDuration" name="contractDuration" value="<%=negotiation.getContractDuration()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>出版單位</td>
                                        <td>
                                            <select id="fkPublisherID" name="fkPublisherID">
                                                <option value=""></option>
                                                <% for (int i = 0; i < publisherList.size(); i++) {
                                                        Publisher p = (Publisher) publisherList.get(i);
                                                        if (negotiation.getFkPublisherId().equals(p.getId())) {
                                                %>
                                                <option value="<%=p.getId()%>" selected="<%=p.getId()%>"><%=p.getName()%></option>
                                                <%} else {%>
                                                <option value="<%=p.getId()%>"><%=p.getName()%></option>
                                                <%}
                                                    }%>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>聯絡人</td>
                                        <td><input type="text" id="contactName" name="contactName" value="<%=negotiation.getContactName()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>聯絡人部門</td>
                                        <td><input type="text" id="contactDepartment" name="contactDepartment" value="<%=negotiation.getContactDepartment()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>聯絡人電話</td>
                                        <td><input type="text" id="contactPhone" name="contactPhone" value="<%=negotiation.getContactPhone()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>聯絡人電子郵件</td>
                                        <td><input type="text" id="contactEmail" name="contactEmail" value="<%=negotiation.getContactEmail()%>"></td>
                                    </tr>
                                    <tr>
                                        <td>合約編號</td>
                                        <td>
                                            <select id="fkContractID" name="fkContractID">
                                                <option value=""></option>
                                                <% for (int i = 0; i < contractList.size(); i++) {
                                                        Contract c = (Contract) contractList.get(i);
                                                        if (negotiation.getFkContractId().equals(c.getId())) {
                                                %>
                                                <option value="<%=c.getId()%>" selected="<%=c.getId()%>"><%=c.getId()%></option>
                                                <%} else {%>
                                                <option value="<%=c.getId()%>"><%=c.getId()%></option>
                                                <%}
                                                    }%>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">備註</td>
                                        <td><textarea id="note" name="note" cols="30" rows="5"><%=negotiation.getNote()%></textarea></td>
                                    </tr>
                                    <tr>
                                        <%if (mode.equals("update")) {%>
                                        <td colspan="2" align="right"> 
                                            <input type="button" value="更新" onclick="doUpdate();" />
                                        </td>
                                        <%} else {%>
                                        <td colspan="2" align="right"> 
                                            <input type="submit" value="新增"/>
                                        </td>
                                        <%}%>
                                    </tr>
                                </table>
                            </form>
                            <div style="clear: both;">&nbsp;</div>
                        </div>

                        <%if (!mode.equals("add")) {%>
                        <p>
                            <a name="records">合作修改紀錄</a>
                        </p>
                        <div id="accordion" style="width:940px;">
                            <% for (int i = negoitemList.size() -1 ; i >=0; i--) {
                                    Negoitem ni = (Negoitem) negoitemList.get(i);
                            %> 
                            <h3><a href="#">#<%=i + 1%>: <%=ni.getNegoDate()%> - <%=Util.trimStringToFixedLength(ni.getContent(), 40)%></a></h3>
                            <div>
                                <p>
                                <lable>日期：&nbsp;<%=ni.getNegoDate()%></lable>
                                </p>
                                <p>
                                    <textarea cols="110" rows="10" readonly><%=ni.getContent()%></textarea>
                                </p>
                                <input type="button" value="刪除合作記錄" onclick="deleteNegoItem('<%=ni.getId()%>');">
                            </div>
                            <%}%>

                            <h3><a href="#">新增</a></h3>
                            <div>
                                <p>
                                <lable>日期：&nbsp;</lable><input type="text" id="negoDate_new" name="negoDate_new" size="8" value="<%=Util.getToday()%>">
                                </p>
                                <p>
                                    <textarea id="memo_new" name="memo_new" cols="120" rows="10">新增後將無法修改！</textarea>
                                </p>
                                <input type="button" value="新增合作記錄" onclick="addNegoItem();">
                            </div>
                        </div>

                        <form id="form2" name="form2" action="../doNegoitem" method="post">
                            <input type="hidden" id="mode" name="mode" value="add"></input>
                            <input type="hidden" id="negoDate" name="negoDate"></input>
                            <input type="hidden" id="memo" name="memo"></input>
                            <input type="hidden" id="fkNegotiationID" name="fkNegotiationID" value="<%=id%>"></input>
                        </form>
                        <%}%>
                        <!-- end #content -->
                        <div style="clear: both;">&nbsp;</div>

                    </div>
                </div>
            </div>
            <!-- end #page -->
        </div>
        <div id="footer">
            <p><%=Context.FOOTER%></p>
        </div>
        <!-- end #footer -->
        <!-- ui-dialog -->
        <div id="dialog_update" style="display: none">
            <p>是否要更新版權徵集？</p>
        </div>

        <div id="dialog_add_negoitem" style="display: none">
            <p>是否要新增合作紀錄？</p>
        </div>

        <div id="dialog_delete_negoitem" style="display: none">
            <p>是否要刪除合作紀錄？</p>
        </div>

        <div id="dialog_message" style="display: none;">
            <p><%=message%></p>
        </div>
    </body>
</html>